using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data;
using BaseDatos.Comun;
using BaseDatos;
using Aplicacion.Base.WinForm;


namespace SCHD
{
    public partial class WFCambioClase : TDV.Controles.Base.WinForm.BaseForm_Tipo01
    {
        private string CodigoUnico="";
        public WFCambioClase()
        {
            InitializeComponent();
        }
        public WFCambioClase(string codigo)
            :this()
        {
            CodigoUnico = codigo;
        }

        private void WFCambioClase_Load(object sender, EventArgs e)
        {
            Categorias();
            Tipos();
            Clases();
        }
        protected void Clases()
        {
            string categoria = CodigoUnico.ToString().Substring(0, 3);
            string tipo = CodigoUnico.ToString().Substring(3, 3);
            DataSet Clase = ConexionBaseDatos.Conexion.CrearDataSet("select codcat,codclase,codtip,dscclase from chdclase where codcat='" + categoria + "' and codtip='" + tipo + "' and flgeli='0' order by codclase", CommandType.Text);
            if (Clase.Tables[0].Rows.Count > 0)
            {
                cboClase.DataSource = Clase.Tables[0];
                cboClase.ValueMember = "codclase";
                cboClase.DisplayMember = "dscclase";
                cboClase.Focus();
                cboClase.SelectedItem = 0;
            }
        }
        public void Categorias()
        {
            string categoria = CodigoUnico.ToString().Substring(0, 3);
            DataSet Categoria = ConexionBaseDatos.Conexion.CrearDataSet("select codcat,dsccat from chdcategoria where codcat='"+categoria +"'and flgeli='0' order by codcat", CommandType.Text);
            BindingSource CDatos = new BindingSource();
            CDatos.DataSource = Categoria.Tables[0];
            cboCategoria.DataSource = CDatos;
            cboCategoria.ValueMember = "codcat";
            cboCategoria.DisplayMember = "dsccat";
            cboCategoria.BindingContext = this.BindingContext;
        }
        protected void Tipos()
        {
            string categoria = CodigoUnico.ToString().Substring(0, 3);
            string tipo = CodigoUnico.ToString().Substring(3, 3);
            DataSet Tipo = ConexionBaseDatos.Conexion.CrearDataSet("select codcat,codtip,dsctip from chdtipo where codcat='" + categoria + "' and codtip='"+tipo+"' and  flgeli='0' order by codtip", CommandType.Text);
            cboTipo.DataSource = Tipo.Tables[0];
            cboTipo.ValueMember = "codtip";
            cboTipo.DisplayMember = "dsctip";
            if (Tipo == null)
            {
                cboClase.DataSource = null;
                cboTipo.Items.Clear();
                cboClase.Items.Clear();
                cboTipo.SelectedValue = "000";
                cboClase.SelectedValue = "000";
                cboClase.Refresh();
            }
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            try
            { 
                DbParametro[] Parametros = new DbParametro[2];
                Parametros[0] = new DbParametro("pcodigounico", CodigoUnico);
                Parametros[1] = new DbParametro("pcodclasenew", cboClase.SelectedValue.ToString());
                Parametros = ConexionBaseDatos.Conexion.EjecutarComando("SETCHDCAMBIOCLASE", Parametros);
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel ;
        }
    }
}

